﻿Public Class clsValidateData
    ''''' last modified on Oct 29, 2014 07.19 local time
    Inherits System.Web.UI.Page

    Private strDayEn As String = ""
    Private strMonEn As String = ""
    Private strYearEn As String = ""

    Public ReadOnly Property strDDEn() As String
        Get
            Return strDayEn
        End Get
    End Property

    Public ReadOnly Property strMMEn() As String
        Get
            Return strMonEn
        End Get
    End Property

    Public ReadOnly Property strYYYYEn() As String
        Get
            Return strYearEn
        End Get
    End Property

    Public Function CheckNull(ByVal sender As Object) As Boolean
        CheckNull = False
        If Trim(sender.text) = "" Then
            ScriptManager.RegisterStartupScript(sender, sender.GetType(), "alertMessage", "window.alert('ต้องทำการกรอกข้อมูลให้ครบถ้วนก่อน');", True)
            sender.Focus()
            Exit Function
        End If
        Return True
    End Function

    Public Function ConcatDate(ByVal strDD As String, ByVal strMM As String, ByVal strYYYY As String) As String
        ConcatDate = ""
        If (Trim(strDD) = "" Or Trim(strMM) = "" Or Trim(strYYYY) = "") Then
            Exit Function
        End If
        strDayEn = strDD
        strMonEn = strMM
        strYearEn = CStr(CInt(strYYYY) - 543)
        Return strDD & "/" & strMM & "/" & strYYYY
    End Function

    Public Function Checkinteger(ByVal sender As Object, Optional ByVal ShowAlert As Boolean = True) As Boolean
        Try
            Checkinteger = False
            If Not (IsNumeric(Format(CDbl(sender.text), "###0"))) Then
                If ShowAlert = True Then
                    ScriptManager.RegisterStartupScript(sender, sender.GetType(), "alertMessage", "window.alert('ต้องกรอกข้อมูลเป็นตัวเลขเท่านั้น');", True)
                    sender.Focus()
                End If
                Exit Function
            End If
        Catch ex As Exception
            If ShowAlert = True Then
                ScriptManager.RegisterStartupScript(sender, sender.GetType(), "alertMessage", "window.alert('ต้องกรอกข้อมูลเป็นตัวเลขเท่านั้น');", True)
                sender.Focus()
            End If
            Return False
        End Try
        sender.text = Format(CDbl(sender.text), "###0")
        Return True
    End Function

    Public Function CheckNumeric_Smartcard(ByVal sender As Object, Optional ByVal ShowAlert As Boolean = True) As Boolean
        Try
            CheckNumeric_Smartcard = False


            If Not (IsNumeric(Format(CDbl(sender.text), "0000000000000"))) Then
                If ShowAlert = True Then
                    ScriptManager.RegisterStartupScript(sender, sender.GetType(), "alertMessage", "window.alert('ต้องกรอกข้อมูลเป็นตัวเลขเท่านั้น');", True)
                    sender.Focus()
                End If
                Exit Function
            End If
        Catch ex As Exception
            If ShowAlert = True Then
                ScriptManager.RegisterStartupScript(sender, sender.GetType(), "alertMessage", "window.alert('ต้องกรอกข้อมูลเป็นตัวเลขเท่านั้น');", True)
                sender.Focus()
            End If
            Return False
        End Try
        sender.text = CStr(Format(CDbl(sender.text), "0000000000000"))
        Return True
    End Function

    Public Function CheckNumeric_Account_no(ByVal sender As Object, Optional ByVal ShowAlert As Boolean = True) As Boolean
        Try
            CheckNumeric_Account_no = False
            If Not (IsNumeric(Format(CDbl(sender.text), "000000000000"))) Then
                If ShowAlert = True Then
                    ScriptManager.RegisterStartupScript(sender, sender.GetType(), "alertMessage", "window.alert('ต้องกรอกข้อมูลเป็นตัวเลขเท่านั้น');", True)
                    sender.Focus()
                End If
                Exit Function
            End If
        Catch ex As Exception
            If ShowAlert = True Then
                ScriptManager.RegisterStartupScript(sender, sender.GetType(), "alertMessage", "window.alert('ต้องกรอกข้อมูลเป็นตัวเลขเท่านั้น');", True)
                sender.Focus()
            End If
            Return False
        End Try
        sender.text = Format(CDbl(sender.text), "000000000000")
        Return True
    End Function

    Public Function CheckDate(ByVal sender As Object, Optional ByVal ShowAlert As Boolean = True) As Boolean
        Try
            CheckDate = False
            If Not (IsDate(CDate(sender.text))) Then
                If ShowAlert = True Then
                    ScriptManager.RegisterStartupScript(sender, sender.GetType(), "alertMessage", "window.alert('วันที่ไม่ถูกต้อง');", True)
                End If
                Exit Function
            End If
        Catch ex As Exception
            If ShowAlert = True Then
                ScriptManager.RegisterStartupScript(sender, sender.GetType(), "alertMessage", "window.alert('วันที่ไม่ถูกต้อง');", True)
            End If
            Return False
        End Try
        Return True
    End Function
    Public Function CheckNumdayInMonth(ByVal sender As Object, Optional ByVal ShowAlert As Boolean = True) As Boolean
        Dim lvsmsgbox As String = "ต้องกรอกข้อมูลเป็นตัวเลขวันที่ 1-31 เท่านั้น"
        Try

            CheckNumdayInMonth = False
            If Not (IsNumeric(Format(CInt(sender.Text), "00"))) Then
                If ShowAlert = True Then
                    ScriptManager.RegisterStartupScript(sender,
                                                        sender.GetType(), "alertMessage",
                                                        "window.alert('" & lvsmsgbox & "');", True)
                    sender.Focus()
                    Exit Function
                End If
            Else
                If CInt(sender.Text) > 31 Then
                    ScriptManager.RegisterStartupScript(sender,
                                                        sender.GetType(), "alertMessage",
                                                        "window.alert('" & lvsmsgbox & "');", True)
                    sender.Focus()
                    Exit Function
                End If

            End If
        Catch ex As Exception
            If ShowAlert = True Then
                ScriptManager.RegisterStartupScript(sender,
                                                    sender.GetType(), "alertMessage",
                                                    "window.alert('" & lvsmsgbox & "');", True)
                sender.Focus()
            End If
            Return False
        End Try
        sender.Text = Format(CDbl(sender.Text), "00")

        Return True
    End Function

    Public Function CheckMonthOfYear(ByVal sender As Object, Optional ByVal ShowAlert As Boolean = True) As Boolean
        Dim lvsmsgbox As String = "ต้องกรอกข้อมูลเป็นตัวเลขเป็นเดือนระหว่าง 1-12 เท่านั้น"
        Try

            CheckMonthOfYear = False
            If Not (IsNumeric(Format(CInt(sender.text), "00"))) Then
                If ShowAlert = True Then
                    ScriptManager.RegisterStartupScript(sender,
                                                        sender.GetType(), "alertMessage",
                                                        "window.alert('" & lvsmsgbox & "');", True)
                    sender.Focus()
                    Exit Function
                End If

            Else
                If CInt(sender.text) > 12 Then
                    ScriptManager.RegisterStartupScript(sender,
                                                        sender.GetType(), "alertMessage",
                                                        "window.alert('" & lvsmsgbox & "');", True)
                    sender.Focus()
                    Exit Function
                End If

            End If
        Catch ex As Exception
            If ShowAlert = True Then
                ScriptManager.RegisterStartupScript(sender,
                                                    sender.GetType(), "alertMessage",
                                                    "window.alert('" & lvsmsgbox & "');", True)
                sender.Focus()
            End If
            Return False
        End Try
        sender.text = Format(CDbl(sender.text), "00")

        Return True
    End Function
    Public Function CheckMonthOfYear_and_Day(ByVal sender01 As Object, ByVal sender02 As Object, Optional ByVal ShowAlert As Boolean = True) As Boolean
        Dim lvsmsgbox As String = "ต้องกรอกข้อมูลเป็นตัวเลขเป็นเดือนระหว่าง 1-12 เท่านั้น"
        Try

            CheckMonthOfYear_and_Day = False
            If Not (IsNumeric(Format(CInt(sender01.text), "00"))) Then
                If ShowAlert = True Then
                    ScriptManager.RegisterStartupScript(sender01,
                                                        sender01.GetType(), "alertMessage",
                                                        "window.alert('" & lvsmsgbox & "');", True)
                    sender01.Focus()
                    Exit Function
                End If

            Else
                If CInt(sender01.text) > 12 Then
                    ScriptManager.RegisterStartupScript(sender01,
                                                        sender01.GetType(), "alertMessage",
                                                        "window.alert('" & lvsmsgbox & "');", True)
                    sender01.Focus()
                    Exit Function
                End If

            End If
        Catch ex As Exception
            If ShowAlert = True Then
                ScriptManager.RegisterStartupScript(sender01,
                                                    sender01.GetType(), "alertMessage",
                                                    "window.alert('" & lvsmsgbox & "');", True)
                sender01.Focus()
            End If
            Return False
        End Try


        sender01.text = Format(CDbl(sender01.text), "00")

        Return True

    End Function
    Public Function CheckYear_TH(ByVal sender As Object, Optional ByVal ShowAlert As Boolean = True) As Boolean
        Dim lvsmsgbox As String = "ต้องกรอกข้อมูลเป็นตัวเลขเป็นปีพ.ศ.ระหว่าง 2300-2999 เท่านั้น"
        Try

            CheckYear_TH = False
            If Not (IsNumeric(Format(CInt(sender.text), "0000"))) Then
                If ShowAlert = True Then
                    ScriptManager.RegisterStartupScript(sender,
                                                        sender.GetType(), "alertMessage",
                                                        "window.alert('" & lvsmsgbox & "');", True)
                    sender.Focus()
                    Exit Function
                End If

            Else
                If CInt(sender.text) < 2300 Or CInt(sender.text) > 2999 Then
                    ScriptManager.RegisterStartupScript(sender,
                                                        sender.GetType(), "alertMessage",
                                                        "window.alert('" & lvsmsgbox & "');", True)
                    sender.Focus()
                    Exit Function
                End If

            End If
        Catch ex As Exception
            If ShowAlert = True Then
                ScriptManager.RegisterStartupScript(sender,
                                                    sender.GetType(), "alertMessage",
                                                    "window.alert('" & lvsmsgbox & "');", True)
                sender.Focus()
            End If
            Return False
        End Try
        sender.text = Format(CDbl(sender.text), "0000")

        Return True
    End Function

End Class